<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <script src="../../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <!-- bootstrap -->
    <link rel="stylesheet" href="../../bootstrap/js/bootstrap.js">
    <link rel="stylesheet" href="../../bootstrap/css/bootstrap.css">

    <title>犬粪采集</title>
    <style>
        /*web background*/
        .container{
            display:table;
            height:100%;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="row" style="padding-top: 20px;margin-top: 20px">
            <div class="col-md-12">
                <div class="box-body">
                    <form role="form">
                        <!-- text input -->
                        <div class="form-group  has-success select-5">
                            <style>
                                @media (min-width: 992px){
                                    .select-5 .form-control{
                                        width:18%;
                                        margin-left:15px;
                                    }
                                }

                            </style>
                            <label style="padding-bottom: 5px">所属区域(*)</label>
                            <div class="row">
                                <select id="select_province" class="form-control col-md-2" style="margin-bottom: 5px">
                                    <!--<option value="-1">请选择</option>-->
                                    <option value="0" style="color: #00a65a">全国</option>
                                    <option value="150000000000">内蒙古自治区</option>
                                    <option value="510000000000">四川省</option>
                                    <option value="530000000000">云南省</option>
                                    <option value="540000000000">西藏自治区</option>
                                    <option value="610000000000">陕西省</option>
                                    <option value="620000000000">甘肃省</option>
                                    <option value="630000000000">青海省</option>
                                    <option value="640000000000">宁夏回族自治区</option>
                                    <option value="650000000000">新疆维吾尔族自治区</option>
                                    <option value="660000000">建设兵团</option>
                                </select>
                                <select id="select_city" class="form-control col-md-2" style="margin-bottom: 5px">
                                    <option value="-1">请选择</option>
                                </select>
                                <select id="select_county" class="form-control col-md-2" style="margin-bottom: 5px"><option value="-1">请选择</option></select>
                                <select id="select_village" class="form-control col-md-2" style="margin-bottom: 5px"><option value="-1">请选择</option></select>
                                <select id="select_hamlet" class="form-control col-md-2" style="margin-bottom: 5px"><option value="-1">请选择</option></select>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <div class="row" style="padding-top: 10px">
            <div class="col-md-6">
                <div class="box-body">
                    <form role="form">
                        <div class="alert alert-info alert-dismissible">
                            <h5><i class="icon fa fa-info"></i>采集犬粪信息</h5>
                        </div>
                        <!-- text input -->
                        <div class="form-group  has-success" style="display: none">
                            <label>id：</label>
                            <input type="text" class="form-control" placeholder="主人id"  id="input_ownerid" />
                        </div>
                        <div class="form-group  has-success">
                            <label>所属村：</label>
                            <input type="text" class="form-control" placeholder="所属村" id="input_dogbelonghamlet" disabled>
                        </div>
                        <div class="form-group  has-success">
                            <label>主人名称：</label>
                            <input type="text" class="form-control" placeholder="主人名称" id="input_ownername">
                        </div>
                        <div class="form-group  has-success">
                            <label>身份证号：</label>
                            <input type="text" class="form-control" placeholder="身份证号" id="input_owneridentity">
                        </div>
                        <div class="form-group  has-success">
                            <label>联系方式：</label>
                            <input type="text" class="form-control" placeholder="联系方式" id="input_telphone">
                        </div>
                        <div class="form-group">
                            <div class="col-md-offset-2 col-md-8">
                                <button type="button" id="a_checkowner" class="btn btn-info btn-flat">主人校验</button>
                                <button type="button" id="a_reset" class="btn btn-info btn-flat">重置</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
            <div class="col-md-6">
                <div class="box-body">
                    <form role="form">
                        <div class="form-group  has-success">
                            <label>犬只政府编号：</label>
                            <select class="form-control select2"  id="select_doggovcode" style="width: 100%;">
                            </select>
                        </div>
                        <div class="form-group  has-success">
                            <label>犬只名称：</label>
                            <input type="text" class="form-control" placeholder="犬只名称" id="input_dogname" disabled>
                        </div>
                        <div class="form-group  has-success">
                            <label>设备编号：</label>
                            <input type="text" class="form-control" placeholder="设备编号" id="input_dogdeviceid" disabled>
                        </div>
                        <div class="form-group  has-success">
                            <label>犬粪编号：</label>
                            <input type="text" class="form-control" placeholder="犬粪编号"  id="input_manurecode" />
                        </div>
                        <div class="form-group  has-success">
                            <label>采集人：</label>
                            <input type="text" class="form-control" placeholder="采集人" id="collection_person">
                        </div>
                        <div class="form-group  has-success">
                            <label>采集时间：</label>
                            <input id="manure_getdate"  class="form-control" type="datetime-local" value="2017-06-30T00:00"/>
                        </div>
                        <div class="form-group">
                            <div class="col-md-offset-2 col-md-8">
                                <button type="button" id="a_addmanure" class="btn btn-info btn-flat">确认采集</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        $(function () {
            //注册相关
            var datares = {};
            $.getJSON ("/aidog/adminlte/pages/ui_js/district.json", function (data)
            {
                datares = data;
            });

            $("#select_province").on('change', function () {
                var selectvalue = $(this).find('option:selected').val();
                selectvalue = selectvalue.substring(0,2);
                $("#select_city").find("option").remove();
                var select_city = document.getElementById("select_city");
                select_city.options.add(new Option("请选择", "-1"));
                if ($('#modalselect_adminlevel').find('option:selected').text()!="省级") {
                    for (var i = 0; i < datares.data1.length; i++) {
                        if (datares.data1[i].districtcode.toString().substring(0, 2) == selectvalue) {
                            //遍历后台传回的结果，一项项往select中添加option
                            select_city.options.add(new Option(datares.data1[i].districtname, datares.data1[i].districtcode));
                        }
                    }
                }
                $("#select_county").find("option").remove();
                var select_county = document.getElementById("select_county");
                select_county.options.add(new Option("请选择", "-1"));

                $("#select_village").find("option").remove();
                var select_village = document.getElementById("select_village");
                select_village.options.add(new Option("请选择", "-1"));

                $("#select_hamlet").find("option").remove();
                var select_hamlet = document.getElementById("select_hamlet");
                select_hamlet.options.add(new Option("请选择", "-1"));
            });
            $("#select_city").on('change', function () {
                var selectvalue = $(this).find('option:selected').val();
                $("#select_county").find("option").remove();
                selectvalue = selectvalue.substring(0, 4);
                var select_county = document.getElementById("select_county");
                select_county.options.add(new Option("请选择", "-1"));
                if ($('#modalselect_adminlevel').find('option:selected').text() != "市级") {
                    for (var i = 0; i < datares.data2.length; i++) {
                        if (datares.data2[i].districtcode.toString().substring(0, 4) == selectvalue) {
                            //遍历后台传回的结果，一项项往select中添加option
                            select_county.options.add(new Option(datares.data2[i].districtname, datares.data2[i].districtcode));
                        }
                    }
                }
                $("#select_village").find("option").remove();
                var select_village = document.getElementById("select_village");
                select_village.options.add(new Option("请选择", "-1"));

                $("#select_hamlet").find("option").remove();
                var select_hamlet = document.getElementById("select_hamlet");
                select_hamlet.options.add(new Option("请选择", "-1"));
            });
            $("#select_county").on('change', function () {
                var selectvalue = $(this).find('option:selected').val();
                $("#select_village").find("option").remove();
                selectvalue = selectvalue.substring(0, 6);
                var select_village = document.getElementById("select_village");
                select_village.options.add(new Option("请选择", "-1"));
                if ($('#modalselect_adminlevel').find('option:selected').text() != "县级") {
                    for (var i = 0; i < datares.data3.length; i++) {
                        if (datares.data3[i].districtcode.toString().substring(0, 6) == selectvalue) {
                            //遍历后台传回的结果，一项项往select中添加option
                            select_village.options.add(new Option(datares.data3[i].districtname, datares.data3[i].districtcode));
                        }
                    }
                }
                $("#select_hamlet").find("option").remove();
                var select_hamlet = document.getElementById("select_hamlet");
                select_hamlet.options.add(new Option("请选择", "-1"));
            });
            $("#select_village").on('change', function () {
                var selectvalue = $(this).find('option:selected').val();
                $("#select_hamlet").find("option").remove();
                selectvalue = selectvalue.substring(0, 9);
                var select_hamlet = document.getElementById("select_hamlet");
                select_hamlet.options.add(new Option("请选择", "-1"));
                if ($('#modalselect_adminlevel').find('option:selected').text() != "乡级") {
                    for (var i = 0; i < datares.data4.length; i++) {
                        if (datares.data4[i].districtcode.toString().substring(0, 9) == selectvalue) {
                            //遍历后台传回的结果，一项项往select中添加option
                            select_hamlet.options.add(new Option(datares.data4[i].districtname, datares.data4[i].districtcode));
                        }
                    }
                }
            });
            var hamletcode = "";
            $("#select_hamlet").on('change', function () {
                hamletcode = $(this).find('option:selected').val();
                var selectvalue = $(this).find('option:selected').text();
                $("#input_dogbelonghamlet").val(selectvalue);
            });

            var ownerid = 0;
            var dogid = 0;
            var managetype = '人工管理';
            $("#a_checkowner").click(function () {
                if(hamletcode == ""){
                    alert("请先选定行政村！");
                    return;
                }
                var clicktype = "ownercheck";
                var ownername = $("#input_ownername").val()==null?"":$("#input_ownername").val();
                var owneridentity = $("#input_owneridentity").val()==null?"":$("#input_owneridentity").val();
                var telphone = $("#input_telphone").val()==null?"":$("#input_telphone").val();
                var checkdata = {};
                checkdata.clicktype = clicktype;
                checkdata.ownername = ownername;
                checkdata.owneridentity = owneridentity;
                checkdata.ownerhamletcode = hamletcode;
                checkdata.telphone = telphone;
                $.ajax({
                    url: "/aidog/api/operateapi",
                    type: "POST",
                    data: JSON.stringify(checkdata),
                    contentType: "application/json",
                    // dataType: "text",    // 控制回来的数据类型
                    beforeSend: function (request) {
                        request.setRequestHeader("token", window.localStorage.getItem("aidog_token"));
                    },
                    success: function (data) {
                        if(data.data == null){
                            alert("信息填写有误或无此人！");
                            return;
                        }
                        alert(data.msg);
                        if(data.success == true){
                            ownerid = data.data.dogowner.ownerId;
                            $("#input_ownerid").val(data.data.dogowner.ownerId);
                            $("#input_ownername").val(data.data.dogowner.ownerName);
                            document.getElementById("input_ownername").readOnly = true;
                            $("#input_owneridentity").val(data.data.dogowner.ownerIdentity);
                            document.getElementById("input_owneridentity").readOnly = true;
                            $("#input_telphone").val(data.data.dogowner.ownerTel);
                            document.getElementById("input_telphone").readOnly = true;

                            var senddata = {};
                            senddata.hamletcode = hamletcode;
                            $.ajax({
                                url:  "/aidog/api/getmanureDogGovcodeList",
                                type: "POST",
                                data:  senddata,
                                beforeSend: function (request) {
                                    request.setRequestHeader("token", window.localStorage.getItem("aidog_token"));
                                },
                                success: function (data) {
                                    var select_doggovcode = document.getElementById("select_doggovcode");
                                    // data.data = objToArray(data.data);
                                    for (var i = 0; i < data.data.govcodelist.length; i++) {
                                        if(data.data.govcodelist[i].ownerName == $("#input_ownername").val()){
                                            //遍历后台传回的结果，一项项往select中添加option
                                            var deviceid = "";
                                            if(data.data.govcodelist[i].necId!="-1"){
                                                deviceid = data.data.govcodelist[i].necId+"--项圈管理";
                                            }else if(data.data.govcodelist[i].appId!="-1"){
                                                deviceid = data.data.govcodelist[i].appId+"--喂饲器管理";
                                            }else{
                                                deviceid = "人工管理";
                                            }
                                            select_doggovcode.options.add(new Option(data.data.govcodelist[i].dogGovcode, data.data.govcodelist[i].dogId+" "+data.data.govcodelist[i].dogName+" "+deviceid));
                                        }
                                    }

                                    var selectvalueinit = $("#select_doggovcode").find('option:selected').val();
                                    dogid = selectvalueinit.split(' ')[0];
                                    $("#input_dogname").val(selectvalueinit.split(' ')[1]);
                                    $("#input_dogdeviceid").val(selectvalueinit.split(' ')[2]);
                                    if(selectvalueinit.split(' ')[2] != "人工管理"){
                                        managetype = selectvalueinit.split(' ')[2].split('--')[1];
                                    }
                                    $("#select_doggovcode").on('change', function () {
                                        var selectvalue = $(this).find('option:selected').val();
                                        dogid = selectvalue.split(' ')[0];
                                        $("#input_dogname").val(selectvalue.split(' ')[1]);
                                        $("#input_dogdeviceid").val(selectvalue.split(' ')[2]);
                                        if(selectvalueinit.split(' ')[2] != "人工管理"){
                                            managetype = selectvalueinit.split(' ')[2].split('--')[1];
                                        }
                                    });
                                }
                            })

                        }else{
                            return;
                        }
                    }
                })
            });

            $("#a_reset").click(function () {
                $("#input_ownerid").val("");
                $("#input_ownername").val("");
                document.getElementById("input_ownername").readOnly = false;
                $("#input_owneridentity").val("");
                document.getElementById("input_owneridentity").readOnly = false;
                $("#input_telphone").val("");
                document.getElementById("input_telphone").readOnly = false;
                var select_doggovcode = document.getElementById("select_doggovcode");
                select_doggovcode.options.length = 0;
                var select_dognecid = document.getElementById("select_dognecid");
                select_dognecid.options.length = 0;
                $("#input_dogname").val("");
            });



            //采集犬粪
            $("#a_addmanure").click(function () {
                var clicktype = "manureadd";
                if(hamletcode == ""){
                    alert("请先选择主人所属行政村！");
                    return;
                }
                var dogmanure_code = $("#input_manurecode").val();
                if(dogmanure_code ==""){
                    alert("未填写犬粪信息！");
                    return;
                }
                if(ownerid == 0){
                    alert("请选择主人！");
                    return;
                }
                var dogowner_id = ownerid;
                var dog_id = dogid;
                var collection_person = $("#collection_person").val();
                var districtcode = hamletcode;
                var manure_getdate = $("#manure_getdate").val()+":00";
                var senddata = {};
                senddata.clicktype = clicktype;
                senddata.dogmanure_code = dogmanure_code;
                senddata.dogowner_id = dogowner_id;
                senddata.dog_id = dog_id;
                senddata.collection_person =  collection_person;
                senddata.districtcode = districtcode;
                senddata.manure_getdate = manure_getdate;
                senddata.managetype = managetype;
                $.ajax({
                    url: "/aidog/api/bindoraddapi",
                    type: "POST",
                    data:  JSON.stringify(senddata),
                    contentType: "application/json",
                    dataType: "text",    // 控制回来的数据类型
                    beforeSend: function (request) {
                        request.setRequestHeader("token", window.localStorage.getItem("aidog_token"));
                    },
                    success: function (data) {
                        alert(data);
                        $("#input_manurecode").val("");
                        $("#collection_person").val("");
                    }
                })
            });

        })


        function objToArray(array) {
            var arr = []
            for (var i in array) {
                arr.push(array[i]);
            }
            return arr;
        }
    </script>
</body>
</html>